/*     */ package com.zimbra.qa.unittest.prov.ldap;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.account.Key.CosBy;
/*     */ import com.zimbra.common.account.Key.DomainBy;
/*     */ import com.zimbra.common.account.Key.ServerBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.Cos;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestProvAttr
/*     */   extends LdapTest
/*     */ {
/*  39 */   private static String TEST_CLASS_NAME = "test-attr";
/*  40 */   private static String PASSWORD = "test123";
/*     */   private static String DOMAIN_NAME;
/*     */   private static String ACCT_EMAIL;
/*     */   private static String SERVER_NAME;
/*     */   private static String COS_NAME;
/*     */   private static Provisioning prov;
/*     */   
/*     */   @BeforeClass
/*     */   public static void init()
/*     */     throws Exception
/*     */   {
/*  51 */     DOMAIN_NAME = baseDomainName();
/*  52 */     ACCT_EMAIL = "user1@" + DOMAIN_NAME;
/*  53 */     SERVER_NAME = "server--" + TEST_CLASS_NAME;
/*  54 */     COS_NAME = "cos--" + TEST_CLASS_NAME;
/*     */     
/*  56 */     prov = Provisioning.getInstance();
/*     */   }
/*     */   
/*     */   private Account createAccount() throws Exception {
/*  60 */     Domain domain = getDomain();
/*  61 */     Cos cos = getCos();
/*     */     
/*  63 */     HashMap<String, Object> attrs = new HashMap();
/*  64 */     attrs.put("zimbraCOSId", cos.getId());
/*  65 */     Account acct = prov.createAccount(ACCT_EMAIL, PASSWORD, attrs);
/*  66 */     Assert.assertNotNull(acct);
/*  67 */     return acct;
/*     */   }
/*     */   
/*     */   private Cos createCos() throws Exception {
/*  71 */     Cos cos = prov.createCos(COS_NAME, new HashMap());
/*  72 */     Assert.assertNotNull(cos);
/*  73 */     return cos;
/*     */   }
/*     */   
/*     */   private Domain createDomain() throws Exception {
/*  77 */     Domain domain = prov.createDomain(DOMAIN_NAME, new HashMap());
/*  78 */     Assert.assertNotNull(domain);
/*  79 */     return domain;
/*     */   }
/*     */   
/*     */   private Server createServer() throws Exception {
/*  83 */     Server server = prov.createServer(SERVER_NAME, new HashMap());
/*  84 */     Assert.assertNotNull(server);
/*  85 */     return server;
/*     */   }
/*     */   
/*     */   private Account getAccount() throws Exception {
/*  89 */     Account acct = prov.get(Key.AccountBy.name, ACCT_EMAIL);
/*  90 */     if (acct == null)
/*  91 */       acct = createAccount();
/*  92 */     Assert.assertNotNull(acct);
/*  93 */     return acct;
/*     */   }
/*     */   
/*     */   private Cos getCos() throws Exception {
/*  97 */     Cos cos = prov.get(Key.CosBy.name, COS_NAME);
/*  98 */     if (cos == null)
/*  99 */       cos = createCos();
/* 100 */     Assert.assertNotNull(cos);
/* 101 */     return cos;
/*     */   }
/*     */   
/*     */   private Domain getDomain() throws Exception {
/* 105 */     Domain domain = prov.get(Key.DomainBy.name, DOMAIN_NAME);
/* 106 */     if (domain == null)
/* 107 */       domain = createDomain();
/* 108 */     Assert.assertNotNull(domain);
/* 109 */     return domain;
/*     */   }
/*     */   
/*     */   private Server getServer() throws Exception {
/* 113 */     Server server = prov.get(Key.ServerBy.name, SERVER_NAME);
/* 114 */     if (server == null)
/* 115 */       server = createServer();
/* 116 */     Assert.assertNotNull(server);
/* 117 */     return server;
/*     */   }
/*     */   
/*     */   private void cannotUnset(Entry entry, String attrName) {
/* 121 */     boolean good = false;
/*     */     try
/*     */     {
/* 124 */       unsetByEmptyString(entry, attrName);
/*     */     } catch (ServiceException e) {
/* 126 */       if (("service.INVALID_REQUEST".equals(e.getCode())) && (e.getMessage().contains("is a required attribute")))
/*     */       {
/* 128 */         good = true; }
/*     */     }
/* 130 */     Assert.assertTrue(good);
/*     */     try
/*     */     {
/* 133 */       unsetByNull(entry, attrName);
/*     */     } catch (ServiceException e) {
/* 135 */       if (("service.INVALID_REQUEST".equals(e.getCode())) && (e.getMessage().contains("is a required attribute")))
/*     */       {
/* 137 */         good = true; }
/*     */     }
/* 139 */     Assert.assertTrue(good);
/*     */   }
/*     */   
/*     */   private void unsetByEmptyString(Entry entry, String attrName) throws ServiceException {
/* 143 */     Map<String, Object> attrs = new HashMap();
/* 144 */     attrs.put(attrName, "");
/* 145 */     prov.modifyAttrs(entry, attrs);
/* 146 */     String newValue = entry.getAttr(attrName, false);
/* 147 */     Assert.assertNull(newValue);
/*     */   }
/*     */   
/*     */   private void unsetByNull(Entry entry, String attrName) throws ServiceException {
/* 151 */     Map<String, Object> attrs = new HashMap();
/* 152 */     attrs.put(attrName, null);
/* 153 */     prov.modifyAttrs(entry, attrs);
/* 154 */     String newValue = entry.getAttr(attrName, false);
/* 155 */     Assert.assertNull(newValue);
/*     */   }
/*     */   
/*     */   private void unsetTest(Entry entry, String attrName) throws ServiceException {
/* 159 */     unsetByEmptyString(entry, attrName);
/* 160 */     unsetByNull(entry, attrName);
/*     */   }
/*     */   
/*     */   private void setAttr(Entry entry, String attrName, String value) throws ServiceException {
/* 164 */     Map<String, Object> attrs = new HashMap();
/* 165 */     attrs.put(attrName, value);
/* 166 */     prov.modifyAttrs(entry, attrs);
/* 167 */     String newValue = entry.getAttr(attrName, false);
/* 168 */     Assert.assertEquals(value, newValue);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testSingleValuedAttr() throws Exception {
/* 173 */     Account acct = getAccount();
/* 174 */     Map<String, Object> attrs = new HashMap();
/*     */     
/* 176 */     String attrName = "zimbraInterceptAddress";
/* 177 */     String V = "test@example.com";
/*     */     
/* 179 */     String value = acct.getAttr(attrName);
/* 180 */     Assert.assertNull(value);
/*     */     
/*     */ 
/* 183 */     attrs.clear();
/* 184 */     attrs.put(attrName, V);
/* 185 */     prov.modifyAttrs(acct, attrs);
/* 186 */     value = acct.getAttr(attrName);
/* 187 */     Assert.assertEquals(V, value);
/*     */     
/*     */ 
/* 190 */     unsetByEmptyString(acct, attrName);
/*     */     
/*     */ 
/* 193 */     attrs.clear();
/* 194 */     attrs.put(attrName, V);
/* 195 */     prov.modifyAttrs(acct, attrs);
/* 196 */     value = acct.getAttr(attrName);
/* 197 */     Assert.assertEquals(V, value);
/*     */     
/*     */ 
/* 200 */     unsetByNull(acct, attrName);
/*     */     
/*     */ 
/* 203 */     attrs.clear();
/* 204 */     attrs.put(attrName, V);
/* 205 */     prov.modifyAttrs(acct, attrs);
/* 206 */     value = acct.getAttr(attrName);
/* 207 */     Assert.assertEquals(V, value);
/*     */     
/*     */ 
/* 210 */     attrs.clear();
/* 211 */     attrs.put("-" + attrName, V);
/* 212 */     prov.modifyAttrs(acct, attrs);
/* 213 */     value = acct.getAttr(attrName);
/* 214 */     Assert.assertNull(value);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testMultiValuedAttr() throws Exception {
/* 219 */     Account acct = getAccount();
/* 220 */     Map<String, Object> attrs = new HashMap();
/*     */     
/* 222 */     String attrName = "zimbraMailForwardingAddress";
/* 223 */     String V1 = "test-1@example.com";
/* 224 */     String V2 = "test-2@example.com";
/*     */     
/*     */ 
/* 227 */     Set<String> values = acct.getMultiAttrSet(attrName);
/* 228 */     Assert.assertTrue((values != null) && (values.size() == 0));
/*     */     
/*     */ 
/* 231 */     attrs.clear();
/* 232 */     attrs.put(attrName, V1);
/* 233 */     prov.modifyAttrs(acct, attrs);
/* 234 */     values = acct.getMultiAttrSet(attrName);
/* 235 */     Assert.assertTrue((values != null) && (values.size() == 1) && (values.contains(V1)));
/*     */     
/*     */ 
/* 238 */     attrs.clear();
/* 239 */     attrs.put("+" + attrName, V2);
/* 240 */     prov.modifyAttrs(acct, attrs);
/* 241 */     values = acct.getMultiAttrSet(attrName);
/* 242 */     Assert.assertTrue((values != null) && (values.size() == 2) && (values.contains(V1)) && (values.contains(V2)));
/*     */     
/*     */ 
/* 245 */     attrs.clear();
/* 246 */     attrs.put("-" + attrName, V1);
/* 247 */     prov.modifyAttrs(acct, attrs);
/* 248 */     values = acct.getMultiAttrSet(attrName);
/* 249 */     Assert.assertTrue((values != null) && (values.size() == 1) && (values.contains(V2)));
/*     */     
/*     */ 
/* 252 */     attrs.clear();
/* 253 */     attrs.put(attrName, "");
/* 254 */     prov.modifyAttrs(acct, attrs);
/* 255 */     String value = acct.getAttr(attrName);
/* 256 */     Assert.assertNull(value);
/* 257 */     values = acct.getMultiAttrSet(attrName);
/* 258 */     Assert.assertTrue((values != null) && (values.size() == 0));
/*     */     
/*     */ 
/* 261 */     attrs.clear();
/* 262 */     attrs.put(attrName, new String[] { V1, V2 });
/* 263 */     prov.modifyAttrs(acct, attrs);
/* 264 */     values = acct.getMultiAttrSet(attrName);
/* 265 */     Assert.assertTrue((values != null) && (values.size() == 2) && (values.contains(V1)) && (values.contains(V2)));
/*     */     
/*     */ 
/* 268 */     attrs.clear();
/* 269 */     attrs.put(attrName, null);
/* 270 */     prov.modifyAttrs(acct, attrs);
/* 271 */     value = acct.getAttr(attrName);
/* 272 */     Assert.assertNull(value);
/* 273 */     values = acct.getMultiAttrSet(attrName);
/* 274 */     Assert.assertTrue((values != null) && (values.size() == 0));
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testDurationAttr() throws Exception {
/* 279 */     Server server = getServer();
/* 280 */     String attrName = "zimbraHsmAge";
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 285 */     Map<String, Object> attrs = new HashMap();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 290 */     attrs.put(attrName, "1d");
/* 291 */     prov.modifyAttrs(server, attrs);
/* 292 */     String strValue = server.getAttr(attrName);
/* 293 */     Assert.assertEquals("1d", strValue);
/* 294 */     long msValue = server.getTimeInterval(attrName, 0L);
/* 295 */     Assert.assertEquals(86400000L, msValue);
/* 296 */     long secValue = server.getTimeIntervalSecs(attrName, 0L);
/* 297 */     Assert.assertEquals(86400L, secValue);
/*     */     
/*     */ 
/* 300 */     attrs.put(attrName, "1h");
/* 301 */     prov.modifyAttrs(server, attrs);
/* 302 */     strValue = server.getAttr(attrName);
/* 303 */     Assert.assertEquals("1h", strValue);
/* 304 */     msValue = server.getTimeInterval(attrName, 0L);
/* 305 */     Assert.assertEquals(3600000L, msValue);
/* 306 */     secValue = server.getTimeIntervalSecs(attrName, 0L);
/* 307 */     Assert.assertEquals(3600L, secValue);
/*     */     
/*     */ 
/* 310 */     attrs.put(attrName, "1m");
/* 311 */     prov.modifyAttrs(server, attrs);
/* 312 */     strValue = server.getAttr(attrName);
/* 313 */     Assert.assertEquals("1m", strValue);
/* 314 */     msValue = server.getTimeInterval(attrName, 0L);
/* 315 */     Assert.assertEquals(60000L, msValue);
/* 316 */     secValue = server.getTimeIntervalSecs(attrName, 0L);
/* 317 */     Assert.assertEquals(60L, secValue);
/*     */     
/*     */ 
/* 320 */     attrs.put(attrName, "1s");
/* 321 */     prov.modifyAttrs(server, attrs);
/* 322 */     strValue = server.getAttr(attrName);
/* 323 */     Assert.assertEquals("1s", strValue);
/* 324 */     msValue = server.getTimeInterval(attrName, 0L);
/* 325 */     Assert.assertEquals(1000L, msValue);
/* 326 */     secValue = server.getTimeIntervalSecs(attrName, 0L);
/* 327 */     Assert.assertEquals(1L, secValue);
/*     */     
/*     */ 
/* 330 */     attrs.put(attrName, "1");
/* 331 */     prov.modifyAttrs(server, attrs);
/* 332 */     strValue = server.getAttr(attrName);
/* 333 */     Assert.assertEquals("1", strValue);
/* 334 */     msValue = server.getTimeInterval(attrName, 0L);
/* 335 */     Assert.assertEquals(1000L, msValue);
/* 336 */     secValue = server.getTimeIntervalSecs(attrName, 0L);
/* 337 */     Assert.assertEquals(1L, secValue);
/*     */     
/*     */ 
/* 340 */     attrs.put(attrName, "1ms");
/* 341 */     prov.modifyAttrs(server, attrs);
/* 342 */     strValue = server.getAttr(attrName);
/* 343 */     Assert.assertEquals("1ms", strValue);
/* 344 */     msValue = server.getTimeInterval(attrName, 0L);
/* 345 */     Assert.assertEquals(1L, msValue);
/* 346 */     secValue = server.getTimeIntervalSecs(attrName, 0L);
/* 347 */     Assert.assertEquals(0L, secValue);
/*     */     
/* 349 */     attrs.put(attrName, "500ms");
/* 350 */     prov.modifyAttrs(server, attrs);
/* 351 */     strValue = server.getAttr(attrName);
/* 352 */     Assert.assertEquals("500ms", strValue);
/* 353 */     msValue = server.getTimeInterval(attrName, 0L);
/* 354 */     Assert.assertEquals(500L, msValue);
/* 355 */     secValue = server.getTimeIntervalSecs(attrName, 0L);
/* 356 */     Assert.assertEquals(1L, secValue);
/*     */     
/* 358 */     attrs.put(attrName, "1000ms");
/* 359 */     prov.modifyAttrs(server, attrs);
/* 360 */     strValue = server.getAttr(attrName);
/* 361 */     Assert.assertEquals("1000ms", strValue);
/* 362 */     msValue = server.getTimeInterval(attrName, 0L);
/* 363 */     Assert.assertEquals(1000L, msValue);
/* 364 */     secValue = server.getTimeIntervalSecs(attrName, 0L);
/* 365 */     Assert.assertEquals(1L, secValue);
/*     */     
/* 367 */     attrs.put(attrName, "1001ms");
/* 368 */     prov.modifyAttrs(server, attrs);
/* 369 */     strValue = server.getAttr(attrName);
/* 370 */     Assert.assertEquals("1001ms", strValue);
/* 371 */     msValue = server.getTimeInterval(attrName, 0L);
/* 372 */     Assert.assertEquals(1001L, msValue);
/* 373 */     secValue = server.getTimeIntervalSecs(attrName, 0L);
/* 374 */     Assert.assertEquals(1L, secValue);
/*     */     
/* 376 */     attrs.put(attrName, "999ms");
/* 377 */     prov.modifyAttrs(server, attrs);
/* 378 */     strValue = server.getAttr(attrName);
/* 379 */     Assert.assertEquals("999ms", strValue);
/* 380 */     msValue = server.getTimeInterval(attrName, 0L);
/* 381 */     Assert.assertEquals(999L, msValue);
/* 382 */     secValue = server.getTimeIntervalSecs(attrName, 0L);
/* 383 */     Assert.assertEquals(1L, secValue);
/*     */     
/* 385 */     attrs.put(attrName, "0ms");
/* 386 */     prov.modifyAttrs(server, attrs);
/* 387 */     strValue = server.getAttr(attrName);
/* 388 */     Assert.assertEquals("0ms", strValue);
/* 389 */     msValue = server.getTimeInterval(attrName, 0L);
/* 390 */     Assert.assertEquals(0L, msValue);
/* 391 */     secValue = server.getTimeIntervalSecs(attrName, 0L);
/* 392 */     Assert.assertEquals(0L, secValue);
/*     */     
/*     */ 
/*     */ 
/* 396 */     boolean good = false;
/* 397 */     attrs.put(attrName, "1y");
/*     */     try {
/* 399 */       prov.modifyAttrs(server, attrs);
/*     */     } catch (AccountServiceException e) {
/* 401 */       if ("account.INVALID_ATTR_VALUE".equals(e.getCode()))
/* 402 */         good = true;
/*     */     }
/* 404 */     Assert.assertTrue(good);
/*     */     
/* 406 */     good = false;
/* 407 */     attrs.put(attrName, "1mm");
/*     */     try {
/* 409 */       prov.modifyAttrs(server, attrs);
/*     */     } catch (AccountServiceException e) {
/* 411 */       if ("account.INVALID_ATTR_VALUE".equals(e.getCode()))
/* 412 */         good = true;
/*     */     }
/* 414 */     Assert.assertTrue(good);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testCallbackAccountStatus() throws Exception
/*     */   {
/* 420 */     Account acct = getAccount();
/* 421 */     String attrName = "zimbraAccountStatus";
/*     */     
/* 423 */     String value = acct.getAttr(attrName);
/* 424 */     Assert.assertEquals("active", value);
/*     */     
/* 426 */     cannotUnset(acct, attrName);
/*     */     
/* 428 */     setAttr(acct, attrName, "closed");
/* 429 */     Assert.assertEquals(acct.getAttr("zimbraMailStatus"), "disabled");
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testCallbackCheckPortConflict() throws Exception {
/* 434 */     Server server = getServer();
/* 435 */     String attrName = "zimbraLmtpBindPort";
/*     */     
/* 437 */     unsetTest(server, attrName);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testCallbackDataSource() throws Exception {
/* 442 */     Account acct = getAccount();
/* 443 */     String attrName = "zimbraDataSourcePollingInterval";
/*     */     
/* 445 */     unsetTest(acct, attrName);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testCallbackDisplayName() throws Exception {
/* 450 */     Account acct = getAccount();
/* 451 */     String attrName = "displayName";
/*     */     
/* 453 */     unsetTest(acct, attrName);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testCallbackDomainStatus() throws Exception {
/* 458 */     Domain domain = getDomain();
/* 459 */     String attrName = "zimbraDomainStatus";
/*     */   }
/*     */   
/*     */ 
/*     */   @Test
/*     */   public void testCallbackMailSignature()
/*     */     throws Exception
/*     */   {
/* 467 */     Account acct = getAccount();
/* 468 */     String attrName = "zimbraPrefMailSignature";
/*     */     
/* 470 */     unsetTest(acct, attrName);
/*     */     
/*     */ 
/* 473 */     Cos cos = getCos();
/* 474 */     setAttr(cos, "zimbraMailSignatureMaxLength", "10");
/*     */     
/*     */ 
/* 477 */     boolean good = false;
/* 478 */     Map<String, Object> attrs = new HashMap();
/* 479 */     attrs.put(attrName, "12345678901");
/*     */     try {
/* 481 */       prov.modifyAttrs(acct, attrs);
/*     */     } catch (ServiceException e) {
/* 483 */       if (("service.INVALID_REQUEST".equals(e.getCode())) && (e.getMessage().contains("is longer than the limited value")))
/*     */       {
/* 485 */         good = true; }
/*     */     }
/* 487 */     Assert.assertTrue(good);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/TestProvAttr.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */